package cafe.adriel.androidaudiorecorder;

import cafe.adriel.androidaudiorecorder.model.AudioChannel;
import cafe.adriel.androidaudiorecorder.model.AudioSampleRate;
import cafe.adriel.androidaudiorecorder.model.AudioSource;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.utils.Color;


public class OhosAudioRecorder {
    /**
     * 常量 EXTRA_FILE_PATH
     */
    public static final String EXTRA_FILE_PATH = "filePath";
    /**
     * 常量 EXTRA_COLOR
     */
    public static final String EXTRA_COLOR = "color";
    /**
     * 常量 EXTRA_SOURCE
     */
    public static final String EXTRA_SOURCE = "source";
    /**
     * 常量 EXTRA_CHANNEL
     */
    public static final String EXTRA_CHANNEL = "Channel";
    /**
     * 常量 EXTRA_SAMPLE_RATE
     */
    public static final String EXTRA_SAMPLE_RATE = "sampleRate";
    /**
     * 常量 EXTRA_AUTO_START
     */
    public static final String EXTRA_AUTO_START = "autoStart";
    /**
     * 常量 EXTRA_KEEP_DISPLAY_ON
     */
    public static final String EXTRA_KEEP_DISPLAY_ON = "keepDisplayOn";
    /**
     * 常量 REQUESTCODE
     */
    public static final int REQUESTCODE = 10000;
    /**
     * 常量 int 是否请求成功 RESULT_OK
     */
    public static final int RESULT_OK = 1;
    /**
     * 常量 int 是否取消 RESULT_CANCELED
     */
    public static final int RESULT_CANCELED = 0;
    private Ability ability;
    private Fraction fraction;

    private String filePath = "";
    private AudioSource source = AudioSource.MIC;
    private AudioSampleRate sampleRate = AudioSampleRate.HZ_44100;
    private int color = Color.getIntColor("#546E7A");
    private boolean autoStart = false;
    private boolean keepDisplayOn = false;

    private OhosAudioRecorder(Ability ability) {
        this.ability = ability;
    }

    private OhosAudioRecorder(Fraction fraction) {
        this.fraction = fraction;
    }

    /**
     * 获取OhosAudioRecorder
     *
     * @param ability
     * @return OhosAudioRecorder
     */
    public static OhosAudioRecorder with(Ability ability) {
        return new OhosAudioRecorder(ability);
    }

    /**
     *
     * 获取OhosAudioRecorder
     *
     * @param fragment
     * @return OhosAudioRecorder
     */
    public static OhosAudioRecorder with(Fraction fragment) {
        return new OhosAudioRecorder(fragment);
    }

    /**
     *
     * 获取OhosAudioRecorder
     *
     * @param filePath
     * @return setFilePath
     */
    public OhosAudioRecorder setFilePath(String filePath) {
        this.filePath = filePath;
        return this;
    }

    /**
     *
     * 获取OhosAudioRecorder
     *
     * @param color
     * @return setColor
     */
    public OhosAudioRecorder setColor(int color) {
        this.color = color;
        return this;
    }

    /**
     *
     * 获取OhosAudioRecorder
     *
     * @param source
     * @return setSource
     */
    public OhosAudioRecorder setSource(AudioSource source) {
        this.source = source;
        return this;
    }

    /**
     *
     * 获取OhosAudioRecorder
     *
     * @param sampleRate
     * @return setSampleRate
     */
    public OhosAudioRecorder setSampleRate(AudioSampleRate sampleRate) {
        this.sampleRate = sampleRate;
        return this;
    }

    /**
     *
     * 获取OhosAudioRecorder
     *
     * @param autoStart
     * @return setAutoStart
     */
    public OhosAudioRecorder setAutoStart(boolean autoStart) {
        this.autoStart = autoStart;
        return this;
    }

    /**
     *
     * 获取OhosAudioRecorder
     *
     * @param keepDisplayOn
     * @return setKeepDisplayOn
     */
    public OhosAudioRecorder setKeepDisplayOn(boolean keepDisplayOn) {
        this.keepDisplayOn = keepDisplayOn;
        return this;
    }

    /**
     *
     * 获取OhosAudioRecorder
     *
     * @param name
     *
     */
    public void record(String name) {
        Intent intent = new Intent();
        intent.setParam(OhosAudioRecorder.EXTRA_FILE_PATH, filePath);
        intent.setParam(OhosAudioRecorder.EXTRA_COLOR, Color.getIntColor("#F4511E"));
        intent.setParam(OhosAudioRecorder.EXTRA_SOURCE, AudioSource.MIC);
        intent.setParam(OhosAudioRecorder.EXTRA_CHANNEL, AudioChannel.MONO);
        intent.setParam(OhosAudioRecorder.EXTRA_SAMPLE_RATE, AudioSampleRate.HZ_48000);
        intent.setParam(OhosAudioRecorder.EXTRA_AUTO_START, false);
        intent.setParam(OhosAudioRecorder.EXTRA_KEEP_DISPLAY_ON, true);
        /*
         * 通过Intent中的OperationBuilder类构造operation对象，指定设备标识（空串表示当前设备）、应用包名、Ability名称
         */
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(ability.getAbilityPackageContext().getBundleName())
                .withAbilityName(name)
                .build();
        intent.setOperation(operation);
        /*
         * 把operation设置到intent中
         */
        ability.startAbilityForResult(intent, OhosAudioRecorder.REQUESTCODE);
    }

    /**
     *
     * 获取recordFromFragment
     *
     * @param name
     *
     */
    public void recordFromFragment(String name) {
        Intent intent = new Intent();
        intent.setParam(EXTRA_FILE_PATH, filePath);
        intent.setParam(EXTRA_COLOR, color);
        intent.setParam(EXTRA_SOURCE, source);
        intent.setParam(EXTRA_SAMPLE_RATE, sampleRate);
        intent.setParam(EXTRA_AUTO_START, autoStart);
        intent.setParam(EXTRA_KEEP_DISPLAY_ON, keepDisplayOn);
        /*
         * 通过Intent中的OperationBuilder类构造operation对象，指定设备标识（空串表示当前设备）、应用包名、Ability名称
         */
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(fraction.getAbilityPackageContext().getBundleName())
                .withAbilityName(name)
                .build();
        /*
         * 把operation设置到intent中
         */
        intent.setOperation(operation);
        fraction.startAbility(intent, REQUESTCODE);
    }


}
